package com.qys.livesMall.app.user.service.impl;

import com.qys.livesMall.app.user.constants.RedisConstant;
import com.qys.livesMall.app.user.model.entity.UserStatisticsDayLog;
import com.qys.livesMall.app.user.mapper.UserStatisticsDayLogMapper;
import com.qys.livesMall.app.user.model.entity.UserStatisticsTeam;
import com.qys.livesMall.app.user.model.entity.UserStatisticsInfo;
import com.qys.livesMall.app.user.service.UserStatisticsDayLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qys.livesMall.app.user.service.UserStatisticsTeamService;
import com.qys.livesMall.app.user.service.UserStatisticsInfoService;
import com.qys.livesMall.cache.RedisCache;
import com.qys.livesMall.cache.utils.CacheUtils;
import com.qys.livesMall.common.utils.DateUtils;
import com.qys.livesMall.finance.common.FinanceLockPrefix;
import com.qys.livesMall.finance.model.dto.user.UserIncomeDayDTO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 今日店铺和用户统计表 服务实现类
 * </p>
 *
 * @author 
 * @since 2023-08-05
 */
@Service
public class UserStatisticsDayLogServiceImpl extends ServiceImpl<UserStatisticsDayLogMapper, UserStatisticsDayLog> implements UserStatisticsDayLogService {
    @Resource
    private UserStatisticsTeamService userStatisticsTeamService;
    @Resource
    private UserStatisticsInfoService userStatisticsInfoService;
    @Resource
    private CacheUtils cacheUtils;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveUserRechargeInfo(UserIncomeDayDTO userIncomeDayDTO) {
        Boolean result=cacheUtils.getLock(FinanceLockPrefix.INCOME_DAY_LOCK+userIncomeDayDTO.getRelevanceId(),CacheUtils.LOCK_WAIT_TIME_60_SECONDS);
        if (result){
            try {
                //当天用户收益数据
                UserStatisticsDayLog userStatisticsDayLog =baseMapper.queryUserRechargeInfo(userIncomeDayDTO);
                //用户团数据
                UserStatisticsTeam userStatisticsTeam = userStatisticsTeamService.getbyUserid(userIncomeDayDTO.getType(),userIncomeDayDTO.getRelevanceId());
                if (null == userStatisticsTeam){
                    return false;
                }
                UserStatisticsInfo userStatisticsInfo=userStatisticsInfoService.selectByUserId(userIncomeDayDTO.getRelevanceId());

                if (null == userStatisticsInfo.getCumulative()){
                    userStatisticsInfo.setCumulative(BigDecimal.ZERO);
                }
                if (null == userStatisticsTeam.getCumulative()){
                    userStatisticsTeam.setCumulative(BigDecimal.ZERO);
                }
                if (null == userStatisticsDayLog){
                    userStatisticsDayLog=new UserStatisticsDayLog();
                    userStatisticsDayLog.setType(userIncomeDayDTO.getType());
                    userStatisticsDayLog.setRelevanceId(userIncomeDayDTO.getRelevanceId());
                    userStatisticsDayLog.setDataTime(DateUtils.dateTimeNow());
                    userStatisticsDayLog.setTodayIncome(BigDecimal.ZERO);
                }
                if (userIncomeDayDTO.getInOut().equals("1")){
                    //1 进账
                    userStatisticsTeam.setCumulative(userStatisticsTeam.getCumulative().add(userIncomeDayDTO.getTodayincome()));
                    userStatisticsDayLog.setTodayIncome(userStatisticsDayLog.getTodayIncome().add(userIncomeDayDTO.getTodayincome()));
                    userStatisticsInfo.setCumulative(userStatisticsInfo.getCumulative().add(userIncomeDayDTO.getTodayincome()));
                }else {
                    //2 出账
                    userStatisticsTeam.setCumulative(userStatisticsTeam.getCumulative().subtract(userIncomeDayDTO.getTodayincome()));
                    userStatisticsDayLog.setTodayIncome(userStatisticsDayLog.getTodayIncome().subtract(userIncomeDayDTO.getTodayincome()));
                    userStatisticsInfo.setCumulative(userStatisticsInfo.getCumulative().subtract(userIncomeDayDTO.getTodayincome()));
                }
                saveOrUpdate(userStatisticsDayLog);
                userStatisticsTeamService.updateById(userStatisticsTeam);
                userStatisticsInfoService.updateById(userStatisticsInfo);
            }finally {
                cacheUtils.releaseLock(FinanceLockPrefix.INCOME_DAY_LOCK+userIncomeDayDTO.getRelevanceId());
            }
        }
        return true;
    }

    @Override
    public List<UserStatisticsDayLog> getbyUserids(List<Long> userids, int type) {
        return baseMapper.getbyUserids(userids,type);
    }

    @Override
    public List<UserStatisticsDayLog> getShopByUserids(List<Long> userids, int type) {
        return baseMapper.getShopByUserids(userids,type);
    }

    @Override
    public BigDecimal getWeekbyUserid(Long userId,Integer type) {
        return baseMapper.getWeekbyUserid(userId,type);
    }

    @Override
    public BigDecimal getDaybyUserid(Long userId,Integer type) {
        return baseMapper.getDaybyUserid(userId,type);
    }

    @Override
    public List<UserStatisticsDayLog> getYesterdayToType(List<Integer> types) {
        return baseMapper.getYesterdayToType(types);
    }

    @Override
    public List<UserStatisticsDayLog> getTodayToType(Integer type) {
        return baseMapper.getTodayToType(type);
    }
}
